package zhengyanze.com.aipoemmax.guide.login;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputEditText;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.SignUpCallback;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import zhengyanze.com.aipoemmax.R;
import zhengyanze.com.aipoemmax.guide.utils.DialogManager;

/**
 * 注册碎片
 * Created by zheng on 2017/8/15.
 */

public class RegisterFragment extends Fragment {
    @BindView(R.id.et_user)
    TextInputEditText etUser;
    @BindView(R.id.et_pass)
    TextInputEditText etPass;
    @BindView(R.id.et_email)
    TextInputEditText etEmail;
    Unbinder unbinder;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_register, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.register, R.id.tologin})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.register:
                register();
                break;
            case R.id.tologin:
                toLogin();
                break;
        }
    }

    /**
     * 去登录
     */
    public void toLogin() {
        FragmentManager manager = getActivity().getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(R.id.fab, new LoginFragment());
        transaction.commit();
    }

    /**
     * 注册
     */
    public void register() {
        String username = etUser.getText().toString();
        String password = etPass.getText().toString();
        String email = etEmail.getText().toString();
        if (username == null || "".equals(username)) {
            Toast.makeText(getContext(), "用户名不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        if (password == null || "".equals(password)) {
            Toast.makeText(getContext(), "密码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        DialogManager.showDialog(getActivity(),"注册中...");
        AVUser user = new AVUser();// 新建 AVUser 对象实例
        user.setUsername(username);// 设置用户名
        user.setPassword(password);// 设置密码
        user.setEmail(email);//设置邮箱
        user.signUpInBackground(new SignUpCallback() {
            @Override
            public void done(AVException e) {
                if (e == null) {
                    // 注册成功，把用户对象赋值给当前用户 AVUser.getCurrentUser()
                    Toast.makeText(getContext(), "注册成功", Toast.LENGTH_SHORT).show();
                    toLogin();
                } else {
                    // 失败的原因可能有多种，常见的是用户名已经存在。
                    Toast.makeText(getActivity(), e.getMessage(), Toast.LENGTH_SHORT).show();
                }
                DialogManager.dismissDialog();
            }
        });
    }
}
